package com.example.baseapp.topmethod

import android.app.Application
import android.content.Context
import android.content.Intent
import android.os.Build
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.io.BufferedReader
import java.io.IOException
import java.io.InputStreamReader
import java.nio.charset.StandardCharsets
import java.util.*
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
import kotlin.coroutines.suspendCoroutine

/**
 * @作者：HuaCon
 *
 * @类描述：顶层方法
 *
 */

/**
 * 跳转工具类
 */
fun AppCompatActivity.skipActivity(clazz: Class<*>) = this.startActivity(Intent(this, clazz))

/**
 * 首字母大写
 */
fun String.firstWordUpCase(): String? {
    //this.toLowerCase(Locale.ROOT).capitalize(Locale.ROOT)
    val lowerStrList = this.toLowerCase(Locale.ROOT).split("_")
    val strBuilder = StringBuilder()
    for (s in lowerStrList) {
        strBuilder.append(s.capitalize())
    }
    return strBuilder.toString()
}

/**
 * 获取某个范围的的随机数
 */
fun IntRange.getRandomNumber() = this.random()


/**
 * 新的知识点：泛型实化
 * 泛型实话在代码编译后可以获取此时泛型的具体类型
 * 1.必须是内联函数
 * 2.必须使用reified关键字声明要对泛型进行实话
 */
inline fun <reified T> AppCompatActivity.openActivity(context: Context) {
    val intent = Intent(context, T::class.java)
    startActivity(intent)
}

inline fun <reified T> AppCompatActivity.openActivity(context: Context, block: Intent.() -> Unit) {
    val intent = Intent(context, T::class.java)
    intent.block()
    startActivity(intent)
}

/**
 * 简化Toast写法
 */
fun String.toast(context: Context) = Toast.makeText(context, this, Toast.LENGTH_SHORT).show()

object ServerCreate {

    val builder: Retrofit by lazy {
        Retrofit.Builder()
            .baseUrl("http://v.juhe.cn/")
            .addConverterFactory(GsonConverterFactory.create())
            .build()
    }

    inline fun <reified T> create(): T = builder.create(T::class.java)

}

suspend inline fun <T> Call<T>.huaCoAwait(): T? {

    return suspendCoroutine { continuation ->
        enqueue(object : Callback<T> {
            override fun onResponse(call: Call<T>, response: Response<T>) {
                continuation.resume(response.body())
            }

            override fun onFailure(call: Call<T>, t: Throwable) {
                continuation.resumeWithException(t)
            }
        })
    }
}


@RequiresApi(Build.VERSION_CODES.KITKAT)
fun Application.readJsonStringFromAssetsFile(jsonFileName: String): String? {
    return try {
        val isr = InputStreamReader(
           this.assets.open(jsonFileName),
            StandardCharsets.UTF_8
        )
        val br = BufferedReader(isr)
        var line: String?
        val builder = java.lang.StringBuilder()
        while (br.readLine().also { line = it } != null) {
            builder.append(line)
        }
        br.close()
        isr.close()
        builder.toString()
    } catch (e: IOException) {
        e.printStackTrace()
        null
    }
}


